package cn.itsource.pethome.system.pet.service.impl;

import cn.itsource.pethome.system.dept.domain.Department;
import cn.itsource.pethome.system.pet.domain.PetType;
import cn.itsource.pethome.system.pet.mapper.PetTypeMapper;
import cn.itsource.pethome.system.pet.service.IPetTypeService;
import cn.itsource.pethome.base.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zcj
 * @since 2023-02-05
 */
@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements IPetTypeService {
    @Autowired
    private PetTypeMapper petTypeMapper;

    @Override
    public List<PetType> typeTree() {
        //第一步查询全部
        List<PetType> petTypes = petTypeMapper.findAll();

        //第三步 儿子找父亲 建立好关系网
        for (PetType petType : petTypes) {
            if(petType.getPid() != null){//找顶级
                for (PetType parent : petTypes) {
                    if(petType.getPid() == parent.getId()){//判断是不是你的父亲
                        List<PetType> children = parent.getChildren();
                        children.add(petType);
                        break;
                    }
                }
            }
        }
        //第二步找顶级
        List<PetType> firstTree = petTypes.stream()
                .filter(petType -> petType.getPid() == null)//过滤出来父id为null
                .collect(Collectors.toList());//转为list

        return firstTree;
    }
}
